// main.rs

#[derive(Debug, Clone, PartialEq, Eq)]
enum State {
    Idle,
    Running,
    Paused,
    Stopped,
}

#[derive(Debug, Clone)]
enum Event {
    Start,
    Pause,
    Resume,
    Stop,
}

struct StateMachine {
    state: State,
}

impl StateMachine {
    fn new(initial: State) -> Self {
        StateMachine { state: initial }
    }

    fn transition(&mut self, event: Event) {
        use State::*;
        use Event::*;

        self.state = match (&self.state, event) {
            (Idle, Start) => Running,
            (Running, Pause) => Paused,
            (Paused, Resume) => Running,
            (Running, Stop) | (Paused, Stop) | (Idle, Stop) => Stopped,
            // 如果事件非法，保持原状态
            (s, _) => s.clone(),
        };
    }

    fn get_state(&self) -> &State {
        &self.state
    }
}

fn main() {
    let mut fsm = StateMachine::new(State::Idle);

    println!("初始状态: {:?}", fsm.get_state());

    fsm.transition(Event::Start);
    println!("收到 Start 事件后: {:?}", fsm.get_state());

    fsm.transition(Event::Pause);
    println!("收到 Pause 事件后: {:?}", fsm.get_state());

    fsm.transition(Event::Resume);
    println!("收到 Resume 事件后: {:?}", fsm.get_state());

    fsm.transition(Event::Stop);
    println!("收到 Stop 事件后: {:?}", fsm.get_state());
}
